package com.free.note.controller;

import com.free.common.core.utils.poi.ExcelUtil;
import com.free.common.core.web.controller.BaseController;
import com.free.common.core.web.domain.AjaxResult;
import com.free.common.core.web.page.TableDataInfo;
import com.free.common.log.annotation.Log;
import com.free.common.log.enums.BusinessType;
import com.free.note.domain.TypeTree;
import com.free.note.service.impl.TypeTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 类型树Controller
 *
 * @author free
 * @date 2023-03-14
 */
@RestController
@RequestMapping("/tree")
public class TypeTreeController extends BaseController {
    @Autowired
    private TypeTreeService typeTreeService;

    /**
     * 删除类型树
     */
    @PostMapping("/getTreeList")
    public AjaxResult getTreeList() {
        return success(typeTreeService.getTreeList());
    }

    /**
     * 查询类型树列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TypeTree typeTree) {
        startPage();
        List<TypeTree> list = typeTreeService.selectTypeTreeList(typeTree);
        return getDataTable(list);
    }

    /**
     * 导出类型树列表
     */
    @Log(title = "类型树", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TypeTree typeTree) {
        List<TypeTree> list = typeTreeService.selectTypeTreeList(typeTree);
        ExcelUtil<TypeTree> util = new ExcelUtil<TypeTree>(TypeTree.class);
        util.exportExcel(response, list, "类型树数据");
    }

    /**
     * 获取类型树详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(typeTreeService.selectTypeTreeById(id));
    }

    /**
     * 新增类型树
     */
    @Log(title = "类型树", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TypeTree typeTree) {
        return toAjax(typeTreeService.insertTypeTree(typeTree));
    }

    /**
     * 修改类型树
     */
    @Log(title = "类型树", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TypeTree typeTree) {
        return toAjax(typeTreeService.updateTypeTree(typeTree));
    }

    /**
     * 删除类型树
     */
    @Log(title = "类型树", businessType = BusinessType.INSERT)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(typeTreeService.deleteTypeTreeByIds(ids));
    }
}
